#! /usr/bin/env python
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2008 jiakuan.net and widenhome.com, All rights reserved.
# 
# Created on 2008-12-2, by Jiakuan Wang.
#

import locale
import py2po
import po2mo
import glob
import os

class Generator:    
    
    ##
    # The constructor of the py2po.Executant class, the method mainly receives 
    # customized parameters when it is being initialized.
    #
    # @param basedir: The base directory of a python project.
    # @param domain: The domain of the i18n, that is, the file name of the PO files.
    # @param localedir: The locale directory for the python application.
    # @param languages: The supported languages for the specific application.
    # @param parameters: The user can pass in the customized parameters.
    # 
    def __init__(self, basedir, domain, localedir, languages, parameters=None):
        self.basedir = basedir
        self.domain =domain
        self.localedir = localedir
        self.languages = languages
        self.parameters = parameters
    
    def execute(self):
        print 'Please input the type of files that you want to generate:'
        print 'po - If you input \'po\', the generator will generate po files.'
        print '''mo - If you input \'mo\', the generator will generate mo files.\nInput type here:'''
            
        option = 'unknown'
        while option.find('po') != 0 and option.find('mo') != 0:
            option = raw_input()
        
        if option.find('po') == 0:
            po_builder = py2po.Executant(self.basedir, self.domain, 
                                         self.localedir, self.languages, 
                                         parameters=self.parameters)
            po_builder.execute()
        else:
            mo_builder = po2mo.Executant(self.domain, self.localedir, 
                                         self.languages)
            mo_builder.execute()

def get_current_language(basedir, localedir, language_setting_loader=None):
    # When there are some problems about language setting, use this variable to
    # define the default language to English, so that we can ensure the 
    # application works correctly anyway.
    default_language = 'en'

    # Load the language setting somewhere, such as configuration files, if there 
    # is no language setting, then set the current language from default locale.
    current_language = locale.getdefaultlocale()[0]
    if language_setting_loader:
        current_language = language_setting_loader()
    
    # Load available languages from locale folder, if the current language is not
    # available language, use the default language
    available_languages = get_available_languages(basedir, localedir)
    if current_language not in available_languages:
        current_language = default_language
    
    return current_language
        
def get_available_languages(basedir, localedir):
    localepath = os.path.abspath(os.path.join(basedir, localedir))
    paths = glob.glob(localepath + '/*')
    languages = []
    for path in paths:
        language = path[len(localepath)+1:]
        languages.append(language)
    return languages

if __name__ == '__main__':
    get_available_languages('D:/MyProject/PythonProject/TestI18N/src','locale/')